// pages/classic/classic.js
// import导入的js文件必须要使用绝对路径
import {ClassicModel} from '../../models/classic.js'
import {LikeModel} from '../../models/like.js'
import {HTTP} from '../../util/http.js'
let classicModel = new ClassicModel()
let likeModel = new LikeModel()
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // classic: []
    _index: 0,
    length: 0,
    _res: [],
    latest:true,
    first:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // http.request({
    //   url: 'users/latest',
    //   method: 'GET',
    //   success:(res)=>{
    //     console.log(res)
    //     this.setData({
    //       classic:res.data
    //     })
    //   }
    // })
    // console.log(classic)
    // 请求页面数据
    classicModel.getLatest((res)=>{
      console.log(res.result)
      this.setData({
        length: res.result.length,
        _res: res.result
      })
      console.log(this.data.length)
      var date = res.result[0].pubdate.split('-')
      var _date = []
      date.forEach(item=>{
        _date.push(parseInt(item))
      })
      console.log(_date)
      this.setData({
        classic: res.result[this.data._index],
        date:_date
      })
    })
  },

  // 点赞处理函数
  onLike(e){
    console.log(e)
    let behaviour = e.detail.behaviour
    console.log(this.data.classic)
    likeModel.like(behaviour,this.data.classic.index)
  },
  prev() {
    let _index = this.data._index
    let _res = this.data._res
    if(_index !== 0){
      _index--
      this.setData({
        classic: _res[_index],
        _index:_index,
        first: false
      })
      if(_index == 0) {
        this.setData({
          latest: true
        })
      }
    }
  },
  next(nextBtn) {
    let _index = this.data._index
    let length = this.data.length
    let _res = this.data._res
    if (_index >= 0 && _index < length-1) {
      _index++
      this.setData({
        classic: _res[_index],
        _index:_index,
        latest: false
      })
      if (_index == length - 1) {
        this.setData({
          first: true
        })
      }
    }
  },
  // getCurrentPages(){
  //   wx.switchTab({
  //     url: "/pages/my/index", success() {
  //       let page = getCurrentPages().pop()
  //       if (page == undefined || page == null) {
  //         return;
  //       }
  //       page.onLoad()
  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})